package com.seveneight.baijiesteward.ui.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.UpdateAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.RankBean;
import com.seveneight.baijiesteward.bean.UpdateBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by user1 on 2018/2/12.
 */

public class UpgradeActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.recyclerview)
    LRecyclerView recyclerview;
    @BindView(R.id.tv_toplevel)
    TextView tv_toplevel;
    private List<RankBean> data;
    private List<UpdateBean> updateData;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_list);
    }

    @Override
    protected void initView() {
        tv_title.setText("立即升级");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            List<RankBean> data = (List<RankBean>) bundle.getSerializable("data");
            updateData = new ArrayList<>();
            for (int i = 0; i < data.size() - 1; i++) {
                RankBean beforeBean = data.get(0);
                RankBean nextBean = data.get(i + 1);
                UpdateBean updateBean = new UpdateBean();
                if (nextBean.getId().equals(setting.getUserInfo().getRankId())) {
                    updateData.clear();
                    continue;
                }
                updateBean.setUpGradeRankID(nextBean.getId());
                updateBean.setNextRankName(nextBean.getRankName());
                updateBean.setUpDatePrice(nextBean.getUpGradeBrokerage());
                updateBean.setNowRankName(bundle.getString("level"));
                updateBean.setNextRankID(nextBean.getId());
                updateData.add(updateBean);
            }
            if(updateData.size()==0){
                tv_toplevel.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void initData() {
        RecyclerviewInitUtil.initLineRecycler(activity, recyclerview);
        recyclerview.setPullRefreshEnabled(false);
        UpdateAdapter updateAdapter = new UpdateAdapter(activity);
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(updateAdapter);
        recyclerview.setAdapter(lRecyclerViewAdapter);
        updateAdapter.addAll(updateData);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode== AppConfig.REQUEST_CODE_0&&resultCode==RESULT_OK){
            setResult(RESULT_OK);
            finish();
        }
    }
}
